package cluster

import (
	apierrors "k8s.io/apimachinery/pkg/api/errors"
	"satellite/pkg/util"
)

type ShowOption struct {
	EnableCondition  bool
	EnableSpec       bool
	EnableAnnotation bool
	EnableLabel      bool
	EnableFinalizer  bool
	EnableWatch      bool
}

type Show struct {
	*ShowOption
	*Base
}

func NewShow(opt *ShowOption) *Show {
	return &Show{
		ShowOption: opt,
	}
}

func (s *Show) WithBase(base *Base) *Show {
	s.Base = base
	return s
}

func (s *Show) Run() error {
	switch s.Type {
	case TypeMysql:
		return s.showMysql()
	case TypeMgr:
		return s.showMgr()
	default:
		err := s.showMysql()
		if apierrors.IsNotFound(err) {
			return s.showMgr()
		}
	}
	return nil
}

func (s *Show) showMysql() error {
	cr, err := s.getMysqlCr()
	if err != nil {
		return err
	}
	switch true {
	case s.EnableCondition:
		util.Println(cr.Status.MGR)
	case s.EnableSpec:
		util.Println(cr.Spec.MGR)
	case s.EnableAnnotation:
		util.Println(cr.Annotations)
	case s.EnableLabel:
		util.Println(cr.Labels)
	case s.EnableFinalizer:
		util.Println(cr.Finalizers)
	default:
		status := cr.Status.MGR
		status.Conditions = nil
		util.Println(status)
	}
	return nil
}

func (s *Show) showMgr() error {
	cr, err := s.getMgrCr()
	if err != nil {
		return err
	}
	switch true {
	case s.EnableCondition:
		util.Println(cr.Status)
	case s.EnableSpec:
		util.Println(cr.Spec)
	case s.EnableAnnotation:
		util.Println(cr.Annotations)
	case s.EnableLabel:
		util.Println(cr.Labels)
	case s.EnableFinalizer:
		util.Println(cr.Finalizers)
	default:
		status := cr.Status
		status.Conditions = nil
		util.Println(status)
	}
	return nil
}
